using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace ScreenManagement
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {
        private SpriteBatch spriteBatch;
        private SpriteFont spriteFont;
        private Texture2D gradient;

        #region Initialization

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
            : base("Masters of Influence")
        {
            // Create our menu entries.
            MenuEntry quickPlayEntry = new MenuEntry("Quick Play");
            MenuEntry singlePlayerMenuEntry = new MenuEntry("Local Game");
            MenuEntry howToPlayMenuEntry = new MenuEntry("How to Play");
            MenuEntry creditsMenuEntry = new MenuEntry("Credits");
            MenuEntry exitMenuEntry = new MenuEntry("Quit");

            // Hook up menu event handlers.
            quickPlayEntry.Selected += QuickPlayMenuEntrySelected;
            singlePlayerMenuEntry.Selected += SinglePlayerMenuEntrySelected;
            howToPlayMenuEntry.Selected += HowToPlayMenuEntrySelected;
            creditsMenuEntry.Selected += CreditsMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(quickPlayEntry);
            MenuEntries.Add(singlePlayerMenuEntry);
            MenuEntries.Add(howToPlayMenuEntry);
            MenuEntries.Add(creditsMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        public override void LoadContent()
        {
            base.LoadContent();

            // Load font for drawing hint
            spriteBatch = new SpriteBatch(this.ScreenManager.GraphicsDevice);

            ContentManager content = new ContentManager(this.ScreenManager.Game.Services, "Content");
            spriteFont = content.Load<SpriteFont>("Fonts/gameFontVerySmall");
            gradient = content.Load<Texture2D>("Textures/Screens/gradient");
        }

        #endregion

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            if (!IsExiting)
            {
                // Draw hint
                spriteBatch.Begin();
                spriteBatch.Draw(gradient, new Rectangle(500, this.ScreenManager.GraphicsDevice.Viewport.Bounds.Height - 330, 280, spriteFont.LineSpacing + 10), new Color(0.5f, 0.5f, 0.5f, 0.5f));
                switch (this.SelectedEntry)
                {
                    case 0:
                        spriteBatch.DrawString(spriteFont, "Play 1 v 1 against AI", new Vector2((this.ScreenManager.GraphicsDevice.Viewport.Bounds.Width / 2) - (spriteFont.MeasureString("Play 1 v 1 against AI").X / 2), this.ScreenManager.GraphicsDevice.Viewport.Bounds.Height - 325), Color.White); break;
                    case 1:
                        spriteBatch.DrawString(spriteFont, "Play a local game", new Vector2((this.ScreenManager.GraphicsDevice.Viewport.Bounds.Width / 2) - (spriteFont.MeasureString("Play a local game").X / 2), this.ScreenManager.GraphicsDevice.Viewport.Bounds.Height - 325), Color.White); break;
                    case 2:
                        spriteBatch.DrawString(spriteFont, "Learn how to play", new Vector2((this.ScreenManager.GraphicsDevice.Viewport.Bounds.Width / 2) - (spriteFont.MeasureString("Learn how to play").X / 2), this.ScreenManager.GraphicsDevice.Viewport.Bounds.Height - 325), Color.White); break;
                    case 3:
                        spriteBatch.DrawString(spriteFont, "Credits", new Vector2((this.ScreenManager.GraphicsDevice.Viewport.Bounds.Width / 2) - (spriteFont.MeasureString("Credits").X / 2), this.ScreenManager.GraphicsDevice.Viewport.Bounds.Height - 325), Color.White); break;
                    case 4:
                        spriteBatch.DrawString(spriteFont, "Exit", new Vector2((this.ScreenManager.GraphicsDevice.Viewport.Bounds.Width / 2) - (spriteFont.MeasureString("Exit").X / 2), this.ScreenManager.GraphicsDevice.Viewport.Bounds.Height - 325), Color.White); break;
                }
                spriteBatch.End();
            }
        }

        #region Handle Input


        /// <summary>
        /// Event handler for when the Single Player menu entry is selected.
        /// </summary>
        void SinglePlayerMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new OptionsMenuScreen(), e.PlayerIndex);
        }

        void HowToPlayMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new HowToPlayScreen(), e.PlayerIndex);
        }

        void CreditsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(new CreditsScreen(), e.PlayerIndex);
        }

        void QuickPlayMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, true, this.ControllingPlayer,
    new GameplayScreen(null, false, this.ControllingPlayer,
        true, null,
        false, null,
        false, null, HexTileBoard.BoardSize.Small));
        }


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            MessageBoxScreen confirmExitMessageBox =
                                    new MessageBoxScreen("Are you sure you want to exit this game?");

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.Game.Exit();
        }


        #endregion
    }
}
